<?php
declare (strict_types = 1);

namespace app\admin\service\data\student;

use app\admin\model\data\PaymentOrderItem;
use app\admin\model\data\Student as StudentStatistic;
use app\admin\model\data\StudentVip;
use app\admin\model\data\UserPaymentData;
use app\admin\service\data\DataService;

/**
 * 数据中心-用户分析
 */
class Student extends DataService
{
    /**
     * 概况
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-21
     * @return   [type]                         [description]
     */
    public function survey()
    {
        $daterange = $this->parseSelectDaterange();
        // 累计用户
        $data['total'] = StudentStatistic::scope('mhm')->count();
        // 新增用户
        $data['register_count'] = StudentStatistic::getRegisterCountByTimespan($daterange['timespan']);
        // 累计付费用户
        $data['payment_user_count_total'] = UserPaymentData::getPaymentUserCountByTimespan([0, time()]);
        // 新增付费用户
        $data['payment_user_count'] = UserPaymentData::getPaymentUserCountByTimespan($daterange['timespan']);
        // 累计会员
        $data['vip_user_count_total'] = StudentVip::scope('mhm', 'user.mhm_id')->withJoin(['user' => ['mhm_id']])->count();
        // 新增会员
        $data['vip_user_count'] = StudentVip::getVipUserCountByTimespan($daterange['timespan']);
        $rows                   = $this->parseRowsDate($daterange['rows']);
        // 统计图表
        if ($daterange['days'] == 1) {
            // 按小时,此处一天内的小时数不会重复,因此可以直接按小时取
            // 新增用户趋势
            $result = StudentStatistic::scope('mhm')->whereBetween('create_time', $daterange['timespan'])->field(['FROM_UNIXTIME(`create_time`,"%H:00")' => 'date', 'count(1)' => 'tp_count'])->group('date')->select()->toArray();
            $rows   = $this->parseRows($result, $rows, 'student');

            // 新增付费用户趋势
            $result = UserPaymentData::scope('mhm', 'user.mhm_id')->withJoin(['user' => ['mhm_id']])->whereBetween('user_payment_data.create_time', $daterange['timespan'])->field(['FROM_UNIXTIME(`user_payment_data`.create_time,"%H:00")' => 'date', 'count(1)' => 'tp_count'])->group('date')->select()->toArray();

            $rows = $this->parseRows($result, $rows, 'payment');
            // 新增会员趋势
            $result = StudentVip::scope('mhm', 'user.mhm_id')->withJoin(['user' => ['mhm_id']])->whereBetween('student_vip.create_time', $daterange['timespan'])->field(['FROM_UNIXTIME(`student_vip`.create_time,"%H:00")' => 'date', 'count(1)' => 'tp_count'])->group('date')->select()->toArray();
            $rows   = $this->parseRows($result, $rows, 'vip');
        } else {
            // 按天
            // 新增用户趋势
            $result = StudentStatistic::scope('mhm')->whereBetween('create_time', $daterange['timespan'])->field(['FROM_UNIXTIME(`create_time`,"%Y-%m-%d")' => 'date', 'count(1)' => 'tp_count'])->group('date')->select()->toArray();
            $rows   = $this->parseRows($result, $rows, 'student');
            // 新增付费用户趋势
            $result = UserPaymentData::scope('mhm', 'user.mhm_id')->withJoin(['user' => ['mhm_id']])->whereBetween('user_payment_data.create_time', $daterange['timespan'])->field(['FROM_UNIXTIME(`user_payment_data`.create_time,"%Y-%m-%d")' => 'date', 'count(1)' => 'tp_count'])->group('date')->select()->toArray();
            $rows   = $this->parseRows($result, $rows, 'payment');
            // 新增会员趋势
            $result = StudentVip::scope('mhm', 'user.mhm_id')->withJoin(['user' => ['mhm_id']])->whereBetween('student_vip.create_time', $daterange['timespan'])->field(['FROM_UNIXTIME(`student_vip`.create_time,"%Y-%m-%d")' => 'date', 'count(1)' => 'tp_count'])->group('date')->select()->toArray();
            $rows   = $this->parseRows($result, $rows, 'vip');
        }

        $data['rows']        = $rows;
        $data['is_platform'] = request()->is_platform;

        return $this->parseToData($data, 1, '获取成功');
    }

    /**
     * 付费用户情况
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-22
     * @return   [type]                         [description]
     */
    public function paymentUser()
    {
        // 解析查询的时间
        $date = $this->parseDate();
        $rows = $this->parseRowsDate($date);
        foreach ($rows as $key => $row) {
            // 查询当前时间段内付费的用户
            $timespan = $date[$row['date']];
            // 这段时间付费的新用户
            $new = UserPaymentData::getPaymentUserCountByTimespan($timespan);
            // 这段时间付费的总用户数量
            $total                          = PaymentOrderItem::getPaymentUserCountByTimespan($timespan);
            $rows[$key]['payment_user_old'] = $total - $new;
            $rows[$key]['payment_user_new'] = $new;
        }

        return $this->parseToData([
            'rows'        => $rows,
            'is_platform' => request()->is_platform,
        ], 1);
    }

    /**
     * 解析统计图指标数据
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-21
     * @param    [type]                         $datas [description]
     * @param    [type]                         $rows  [description]
     * @param    [type]                         $field [description]
     * @return   [type]                                [description]
     */
    protected function parseRows($datas, $rows = [], $field)
    {
        $result = array_column($datas, null, 'date');

        foreach ($rows as $key => $row) {
            $time               = $row['date'];
            $rows[$key][$field] = isset($result[$time]) ? $result[$time]['tp_count'] : 0;
        }

        return $rows;
    }
}
